/*
	Personal Bug Tracker (www.personalbugtracker.com)
	Copyright (C) 2014 Sergey Nechaev <serg.nechaev@gmail.com>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package com.personalbugtracker.ui.controller;


import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.HBox;

import com.personalbugtracker.PersonalBugTracker;
import com.personalbugtracker.domain.Issue;
import com.personalbugtracker.domain.Project;

public class MainFrameFXController {

	@FXML
	// ResourceBundle that was given to the FXMLLoader
	private ResourceBundle resources;

	@FXML
	// URL location of the FXML file that was given to the FXMLLoader
	private URL location;

	@FXML
	// fx:id="btn_search"
	private Button btn_search; // Value injected by FXMLLoader

	@FXML
	// fx:id="lbl_status"
	private Label lbl_status; // Value injected by FXMLLoader

	@FXML
	// fx:id="tf_search"
	private TextField tf_search; // Value injected by FXMLLoader

	@FXML
	// fx:id="hbox_status"
	private HBox hbox_status; // Value injected by FXMLLoader

	@FXML
	// fx:id="split_main"
	private SplitPane split_main; // Value injected by FXMLLoader

	@FXML
	// fx:id="tree_projects"
	private TreeView<Project> tree_projects; // Value injected by FXMLLoader

	private Node editProjectNode;
	private Node listOfProjectBugs;

	private TreeView<Project> tree;
	private TreeItem<Project> rootItem;

	@FXML
	// This method is called by the FXMLLoader when initialization is complete
	void initialize() {
		assert btn_search != null : "fx:id=\"btn_search\" was not injected: check your FXML file 'main-frame.fxml'.";
		assert lbl_status != null : "fx:id=\"lbl_status\" was not injected: check your FXML file 'main-frame.fxml'.";
		assert tf_search != null : "fx:id=\"tf_search\" was not injected: check your FXML file 'main-frame.fxml'.";
		assert hbox_status != null : "fx:id=\"hbox_status\" was not injected: check your FXML file 'main-frame.fxml'.";
		assert split_main != null : "fx:id=\"split_main\" was not injected: check your FXML file 'main-frame.fxml'.";
		assert tree_projects != null : "fx:id=\"tree_projects\" was not injected: check your FXML file 'main-frame.fxml'.";
	}

	public void onNewProject(Event e) {

		if (editProjectNode == null) {

			URL location = getClass().getResource("/fxml/edit-project.fxml");

			FXMLLoader fxmlLoader = new FXMLLoader();
			fxmlLoader.setLocation(location);
			fxmlLoader.setResources(ResourceBundle.getBundle("i18n.EditProjectBundle"));
			fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());

			try {
				editProjectNode = (Node) fxmlLoader.load(location.openStream());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		setRightComponent(editProjectNode);
	}

	public void setRightComponent(Node node) {
		this.split_main.getItems().set(1, node);
	}

	public void loadProjectsTree() {

		if (tree == null) {

			rootItem = new TreeItem<Project>(new Project());
			rootItem.setExpanded(true);
			tree = new TreeView<Project>(rootItem);
			split_main.getItems().set(0, tree);

			tree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<Project>>() {

				@Override
				public void changed(ObservableValue<? extends TreeItem<Project>> observable, TreeItem<Project> oldValue, TreeItem<Project> newValue) {
					loadProjectBugs(newValue.getValue());
				}
			});

		} else {
			rootItem.getChildren().clear();
		}

		List<Project> list = PersonalBugTracker.services.getProjectService().findAll();

		for (Project p : list) {
			TreeItem<Project> item = new TreeItem<Project>(p);
			rootItem.getChildren().add(item);
		}

	}

	protected void loadProjectBugs(Project value) {
		
		ObservableList list = new ObservableList<Issue>(){};
		
		List<Issue> list = PersonalBugTracker.services.getIssueService().findByProject(project);
		
		list.addAll(list);
		
		listOfProjectBugs = new TableView(list);
		
		setRightComponent(listOfProjectBugs);

	}

}
